package com.l024.android_ljl.view.main.activity;

import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.hjq.toast.ToastUtils;
import com.l024.android_ljl.R;
import com.l024.android_ljl.constant.PageConstant;
import com.l024.android_ljl.databinding.ActivityCollectionBinding;
import com.l024.android_ljl.entity.SysOptionEntity;
import com.l024.android_ljl.entity.SysSubjectEntity;
import com.l024.android_ljl.entity.SysUserSaveEntity;
import com.l024.android_ljl.viewmodel.main.SubjectViewModel;
import com.quantstack.llcommonlibrary.adapter.CommonAdapter;
import com.quantstack.llcommonlibrary.adapter.CommonViewHolder;
import com.quantstack.llcommonlibrary.base.BaseActivity;
import com.quantstack.llcommonlibrary.base.BaseViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏
 */
public class CollectionActivity extends BaseActivity<SubjectViewModel, ActivityCollectionBinding> implements BaseViewModel.OnPageDataListener {
    private List<SysUserSaveEntity> list = new ArrayList<>();
    private  CommonAdapter<SysUserSaveEntity> adapter;
    @Override
    public int getLayoutId() {
        return R.layout.activity_collection;
    }

    @Override
    public void afterCreate() {
       initList();
       mViewModel.setBaseView(this);
       mViewModel.setOnPageDataListener(this);
       //获取用户收藏
       mViewModel.getUserCollection();
    }

    private void initList() {
        adapter = new CommonAdapter<SysUserSaveEntity>(list, new CommonAdapter.OnBindDataListener<SysUserSaveEntity>() {
            @Override
            public void onBindViewHolder(SysUserSaveEntity model, CommonViewHolder viewHolder, int type, int position) {
                SysSubjectEntity subject = model.getSubject();
               if(subject!=null){
                   viewHolder.setText(R.id.tv_title,subject.getTitle());
                   viewHolder.setText(R.id.tv_answer,"答案:"+subject.getAnswer());
                   viewHolder.setText(R.id.tv_note,"解析:"+subject.getNote());
                   LinearLayout llOptions = viewHolder.getView(R.id.ll_options);
                   List<SysOptionEntity> options = subject.getOptions();
                   if(options!=null&&options.size()>0){
                       llOptions.removeAllViews();
                       for(SysOptionEntity optionEntity:options){
                           TextView view = new TextView(CollectionActivity.this);
                           view.setText(optionEntity.getContent());
                           view.setTextSize(18);
                           view.setTextColor(getResources().getColor(R.color.c333));
                           llOptions.addView(view);
                       }
                   }
               }
            }

            @Override
            public int getLayoutId(int type) {
                return R.layout.item_collection;
            }
        });
        mViewDataBind.rlCollections.setLayoutManager(new LinearLayoutManager(this));
        mViewDataBind.rlCollections.setAdapter(adapter);
    }

    @Override
    public void onSuccess(String tag) {
        switch (tag){
            case PageConstant.USER_COLLECTIONS:{
                List<SysUserSaveEntity> value = mViewModel.getUserCollections().getValue();
                list.clear();
                list.addAll(value);
                adapter.notifyDataSetChanged();
                break;
            }
        }
    }

    @Override
    public void onError(String tag, String msg) {
        switch (tag){
            case PageConstant.USER_COLLECTIONS:{
                ToastUtils.show(msg);
                break;
            }
        }
    }
}